// pages/my/mypage/myadd/myadd.js
const app = getApp()
const $ = app.globalData
Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		navgat: false
	},

	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function(options) {
		var that = this
		var navgat = options.navgat
		that.setData({
			navgat: navgat
		})
		wx.getStorage({
			key: "userinfo",
			success: function(res) {
				that.setData({
					userinfo: res.data
				})
				wx.request({
					url: $.AdminUrl + "/YApi/getMyAddress",
					data: {
						customerid: res.data.id
					},
					success: function(data) {
						that.setData({
							data: data.data.obj,
						})
						// console.log(data)
					}
				})
			}
		})

	},

	// 新增收货地址
	newadd: function() {
		wx.navigateTo({
			url: "newadd/newadd"
		})
	},
	//删除
	shanchu: function(e) {
		var that = this
		var thisid = e.currentTarget.dataset.id
		var thisindex = e.currentTarget.dataset.ind
		wx.request({
			url: $.AdminUrl + "/YApi/deleteMyAddress",
			data: {
				addressid: thisid
			},
			success: function(data) {
				if (data.data.code) {
					var arr = that.data.data
					arr.splice(thisindex, 1);
					console.log(arr)
					wx.showToast({
						title: '删除成功',
						icon: 'success',
						duration: 2000,
						success: function() {
							that.setData({
								data: arr
							})
						}
					})
				} else {
					wx.showToast({
						title: '删除失败',
						icon: 'error',
						duration: 2000
					})
				}
				console.log(data)
			}
		})
	},
	// 编辑收货地址
	bianji: function(e) {
		var that = this
		var thisid = e.currentTarget.dataset.id
		wx.navigateTo({
			url: "newadd/newadd?id=" + thisid
		})
	},
	// 设为默认地址
	moren: function(e) {
		var that = this
		var thisid = e.currentTarget.dataset.id
		console.log(that.data.userinfo)
		wx.request({
			url: $.AdminUrl + "/YApi/setMyDefaultAddress",
			data: {
				customerid: that.data.userinfo.id,
				addressid: thisid
			},
			success: function(data) {
				if (data.data.code) {
					that.onShow()
				}
			}
		})
	},

	// 返回结账
	fanhuijiez: function(option) {
		var that = this
		if (this.data.navgat) {
			var thisid = option.currentTarget.dataset.id
			wx.setStorage({
				key: "addid",
				data: thisid,
				success: () => {
					wx.navigateBack({})
				}
			})
		}
	},
	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady: function() {

	},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow: function() {
		var that = this
		wx.getStorage({
			key: "userinfo",
			success: function(res) {
				that.setData({
					userinfo: res.data
				})
				wx.request({
					url: $.AdminUrl + "/YApi/getMyAddress",
					data: {
						customerid: res.data.id
					},
					success: function(data) {
						that.setData({
							data: data.data.obj,
						})
						// console.log(data)
					}
				})
			}
		})
	},

	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide: function() {

	},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload: function() {

	},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh: function() {

	},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom: function() {

	},
// 分享转发
		onShareAppMessage: function() {
			var userinfo = wx.getStorageSync('userinfo')
			return {
				title: '优悦购生活管家',
				path: '/pages/index/index?userid='+userinfo.loginname,
				success: (res) => {
					console.log("转发成功", res);
				},
				fail: (res) => {
					console.log("转发失败", res);
				}
			}
		}
})
